﻿#include "lvgl_refresh.h"
#include "img_refresh.h"



_lvgl_refresh_data lvgl_refresh_data;


void refresh_anim_out_ready_cb(lv_anim_t * anim);

void lvgl_refresh_taskcb(lv_task_t *t)
{

	static int cnt=0;
	if (++lvgl_refresh_data.image_cnt > 15)
	{
		lvgl_refresh_data.image_cnt = 0;
	}
	if(lvgl_refresh_data.image)
	{
		lv_img_set_src(lvgl_refresh_data.image, img_refresh_buf[lvgl_refresh_data.image_cnt]);
	}

}


void lvgl_refresh_event_cb(struct _lv_obj_t* obj, lv_event_t event)
{
    if (obj == lvgl_refresh_data.cont_main)
    {
        switch (event)
        {

       		case LV_EVENT_KEY:

            switch (*(const uint32_t*)lv_event_get_data())
            {
            case LV_KEY_UP:
                lvgl_refresh_debug("LV_KEY_UP");

                break;
            case LV_KEY_DOWN:
                lvgl_refresh_debug("LV_KEY_DOWN");

                break;
            case LV_KEY_LEFT:
                lvgl_refresh_debug("LV_KEY_LEFT");

                break;
            case LV_KEY_RIGHT:
                lvgl_refresh_debug("LV_KEY_RIGHT");


                break;
            case LV_KEY_ESC:
                lvgl_refresh_debug("LV_KEY_ESC");


                break;
            case LV_KEY_DEL:
                lvgl_refresh_debug("LV_KEY_DEL");

                break;
            case LV_KEY_BACKSPACE:
                lvgl_refresh_debug("LV_KEY_BACKSPACE");

                break;
            case LV_KEY_ENTER:
                lvgl_refresh_debug("LV_KEY_ENTER");

                break;
            case LV_KEY_NEXT:
                lvgl_refresh_debug("LV_KEY_NEXT");

                break;
            case LV_KEY_PREV:
                lvgl_refresh_debug("LV_KEY_PREV");

                break;
            case LV_KEY_HOME:
                lvgl_refresh_debug("LV_KEY_HOME");

                break;
            case LV_KEY_END:
                lvgl_refresh_debug("LV_KEY_END");
                break;
            }
            break;
        case LV_EVENT_DELETE:

            lvgl_refresh_data.cont_main = NULL;
            lvgl_refresh_debug("delete win");
            break;

        case LV_EVENT_PRESSING://对象被持续按下

            break;
        case LV_EVENT_PRESSED://对象已被按下

            break;
        case LV_EVENT_DRAG_END://拖动结束后


            break;
        case LV_EVENT_RELEASED://按钮释放

        case LV_EVENT_LONG_PRESSED://按钮长按
            break;
        default:
            break;
        }
    }
}
void lvgl_refresh_start(lv_obj_t ** Fu,int * dat,int time)
{
	lvgl_refresh_data.user_data=dat;
	lvgl_refresh_data.fu = Fu;

	if (lvgl_refresh_data.cont_main == NULL)
	{

#if REFRESH_STYLE
        //----创建容器----//
        lvgl_refresh_data.cont_main = lv_cont_create(lv_scr_act(), NULL);
        lv_obj_set_size(lvgl_refresh_data.cont_main, 100, 100);
        lv_obj_set_pos(lvgl_refresh_data.cont_main, 0, lv_obj_get_height(lv_scr_act()));
        lv_obj_set_click(lvgl_refresh_data.cont_main, true); //启用 / 禁用可点击
        lv_obj_set_drag(lvgl_refresh_data.cont_main, true);//启用/禁用对象可拖动
        lv_obj_set_drag_throw(lvgl_refresh_data.cont_main, true);//启用/禁用拖动后是否有惯性移动
        lv_obj_set_drag_parent(lvgl_refresh_data.cont_main, false); //启用 / 禁用父对象可拖动
        lv_obj_set_drag_dir(lvgl_refresh_data.cont_main, LV_DRAG_DIR_HOR);//设置拖动方向
        lv_obj_add_style(lvgl_refresh_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_style.cont_no_border);
		lv_obj_set_style_local_bg_opa(lvgl_refresh_data.cont_main,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_OPA_60);
		lv_obj_set_style_local_radius(lvgl_refresh_data.cont_main, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,20);
		lv_obj_set_event_cb(lvgl_refresh_data.cont_main, lvgl_refresh_event_cb);
		lv_obj_align(lvgl_refresh_data.cont_main, NULL, LV_ALIGN_CENTER, 0, 0);
		lvgl_refresh_data.cont_main_y = lv_obj_get_y(lvgl_refresh_data.cont_main);
		lv_obj_set_y(lvgl_refresh_data.cont_main,lv_obj_get_height(lv_scr_act()));
        //----创建容器----//

		//预加载器
		lvgl_refresh_data.spinner = lv_spinner_create(lvgl_refresh_data.cont_main, NULL);
		lv_obj_set_size(lvgl_refresh_data.spinner, 80, 80);//设置大小
		lv_obj_align(lvgl_refresh_data.spinner, NULL, LV_ALIGN_CENTER, 0, 0);//与屏幕居中对齐
		//设置样式
		lv_spinner_set_arc_length(lvgl_refresh_data.spinner, 45);//设置小圆弧对应的角度
		lv_spinner_set_dir(lvgl_refresh_data.spinner, LV_SPINNER_DIR_FORWARD);//设置为逆时针旋转
		//设置旋转动画的方式
		lv_spinner_set_type(lvgl_refresh_data.spinner, LV_SPINNER_TYPE_SPINNING_ARC);
		lv_spinner_set_spin_time(lvgl_refresh_data.spinner, 1000);//设置旋转的速度 ,转一圈所需要的时间
		//底部园样式
		lv_obj_add_style(lvgl_refresh_data.spinner, LV_SPINNER_TYPE_SPINNING_ARC, &lvgl_style.lvgl_preload_spinning_style);
		//旋转的弧样式
		lv_obj_add_style(lvgl_refresh_data.spinner, LV_SPINNER_TYPE_FILLSPIN_ARC, &lvgl_style.lvgl_preloadfillspin_style);
		lv_obj_set_style_local_line_color(lvgl_refresh_data.spinner,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x83, 0x83, 0x83));
#else
        //----创建容器----//
        lvgl_refresh_data.cont_main = lv_cont_create(lv_scr_act(), NULL);
        lv_obj_set_size(lvgl_refresh_data.cont_main, lv_obj_get_width(lv_scr_act()),  img_refresh_buf[0]->header.h);
        lv_obj_set_pos(lvgl_refresh_data.cont_main, 0, 0);
        lv_obj_add_style(lvgl_refresh_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_style.cont_no_border);
		lv_obj_align(lvgl_refresh_data.cont_main, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
		lv_obj_move_background(lvgl_refresh_data.cont_main);
		lv_obj_set_event_cb(lvgl_refresh_data.cont_main, lvgl_refresh_event_cb);
        //----创建容器----//

		lvgl_refresh_data.image = lv_img_create(lvgl_refresh_data.cont_main, NULL);
		lv_img_set_src(lvgl_refresh_data.image, img_refresh_buf[0]);
		lv_obj_align(lvgl_refresh_data.image, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);

		lvgl_task_create(&lvgl_refresh_data.task,lvgl_refresh_taskcb, 100, LV_TASK_PRIO_HIGH, NULL);
#endif

        lvgl_refresh_debug("create win\r\n");
	}else
    {
        lvgl_refresh_debug("show win\r\n");
    }

	lvgl_group_save_focus_obj();

    lvgl_group_add_obj(&lvgl_refresh_data.cont_main);
    lvgl_group_focus_obj(&lvgl_refresh_data.cont_main);

	lvgl_refresh_data.status=lvgl_refresh_Zhong;
	*lvgl_refresh_data.user_data=true;

	lvgl_hint_set_top();

    refresh_anim_in_start();

#if USE_WIN==0
	system_give_https_request_Semaphore();
#endif
}

void lvgl_refresh_close(void)
{
	if(lvgl_refresh_data.cont_main == NULL)
	{
		return;
	}
    lvgl_refresh_debug("start close");
	refresh_anim_out();
}

void refresh_anim_in_ready_cb(lv_anim_t * anim)
{
	lv_anim_del(&lvgl_refresh_data.anim_in,NULL);
}

void refresh_anim_in_cb(void * arc, lv_anim_value_t v)
{
	lv_obj_set_y(arc, v);
}


//滑入动画
void refresh_anim_in_start(void)
{

	lvgl_refresh_debug("start in anim");
#if REFRESH_STYLE

	lv_anim_path_init(&lvgl_refresh_data.anim_path_in);
	lv_anim_path_set_cb(&lvgl_refresh_data.anim_path_in, lv_anim_path_ease_out);
	//lv_anim_path_linear线性动画
	//lv_anim_path_step最后一步更改
	//lv_anim_path_ease_in开头很慢
	//lv_anim_path_ease_out最后慢
	//lv_anim_path_ease_in_out在开始和结束时也很慢
	//lv_anim_path_overshoot超出最终值
	//lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）

	lv_anim_init(&lvgl_refresh_data.anim_in);
	lv_anim_set_ready_cb(&lvgl_refresh_data.anim_in, refresh_anim_in_ready_cb);
	lv_anim_set_exec_cb(&lvgl_refresh_data.anim_in, refresh_anim_in_cb);
	lv_anim_set_values(&lvgl_refresh_data.anim_in, lv_obj_get_y(lvgl_refresh_data.cont_main), lvgl_refresh_data.cont_main_y);
	lv_anim_set_time(&lvgl_refresh_data.anim_in, 400);
	lv_anim_set_var(&lvgl_refresh_data.anim_in, lvgl_refresh_data.cont_main);

	lv_anim_set_path(&lvgl_refresh_data.anim_in, &lvgl_refresh_data.anim_path_in);
	lv_anim_start(&lvgl_refresh_data.anim_in);
#else
	lv_anim_path_init(&lvgl_refresh_data.anim_path_in);
	lv_anim_path_set_cb(&lvgl_refresh_data.anim_path_in, lv_anim_path_ease_out);
	//lv_anim_path_linear线性动画
	//lv_anim_path_step最后一步更改
	//lv_anim_path_ease_in开头很慢
	//lv_anim_path_ease_out最后慢
	//lv_anim_path_ease_in_out在开始和结束时也很慢
	//lv_anim_path_overshoot超出最终值
	//lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）

	lv_anim_init(&lvgl_refresh_data.anim_in);
	lv_anim_set_ready_cb(&lvgl_refresh_data.anim_in, refresh_anim_in_ready_cb);
	lv_anim_set_exec_cb(&lvgl_refresh_data.anim_in, refresh_anim_in_cb);
	lv_anim_set_values(&lvgl_refresh_data.anim_in, lv_obj_get_y(*lvgl_refresh_data.fu), img_refresh_buf[0]->header.h);
	lv_anim_set_time(&lvgl_refresh_data.anim_in, 200);
	lv_anim_set_var(&lvgl_refresh_data.anim_in, *lvgl_refresh_data.fu);

	lv_anim_set_path(&lvgl_refresh_data.anim_in, &lvgl_refresh_data.anim_path_in);
	lv_anim_start(&lvgl_refresh_data.anim_in);
#endif
}

void refresh_anim_out_ready_cb(lv_anim_t * anim)
{
	lvgl_group_remove_obj(&lvgl_refresh_data.cont_main);

    lvgl_task_delete(&lvgl_refresh_data.task);
	lvgl_obj_del(&lvgl_refresh_data.cont_main);

	lvgl_refresh_data.status=lvgl_refresh_JieShu;
	*lvgl_refresh_data.user_data=false;

	lvgl_send_event(lvgl_refresh_data.fu, CZ_EVENT_REFRESH_OVER,NULL);
	lvgl_group_focus_save_obj();

	lvgl_refresh_debug("refresh closed\r\n");
}

void refresh_anim_out_cb(void * arc, lv_anim_value_t v)
{
	lv_obj_set_y(arc, v);
}

//滑出动画
void refresh_anim_out(void)
{
	lvgl_refresh_debug("start out anim");
	lvgl_group_remove_obj(&lvgl_refresh_data.cont_main);

#if REFRESH_STYLE

	lv_anim_path_init(&lvgl_refresh_data.anim_path_out);
	lv_anim_path_set_cb(&lvgl_refresh_data.anim_path_out, lv_anim_path_ease_out);
	//lv_anim_path_linear线性动画
	//lv_anim_path_step最后一步更改
	//lv_anim_path_ease_in开头很慢
	//lv_anim_path_ease_out最后慢
	//lv_anim_path_ease_in_out在开始和结束时也很慢
	//lv_anim_path_overshoot超出最终值
	//lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）

	lv_anim_init(&lvgl_refresh_data.anim_out);
	lv_anim_set_ready_cb(&lvgl_refresh_data.anim_out, refresh_anim_out_ready_cb);
	lv_anim_set_exec_cb(&lvgl_refresh_data.anim_out, refresh_anim_out_cb);
	lv_anim_set_values(&lvgl_refresh_data.anim_out, lv_obj_get_y(lvgl_refresh_data.cont_main), lv_obj_get_height(lv_scr_act()));
	lv_anim_set_time(&lvgl_refresh_data.anim_out, 400);
	lv_anim_set_var(&lvgl_refresh_data.anim_out, lvgl_refresh_data.cont_main);

	lv_anim_set_path(&lvgl_refresh_data.anim_out, &lvgl_refresh_data.anim_path_out);
	lv_anim_start(&lvgl_refresh_data.anim_out);

#else

	lv_anim_path_init(&lvgl_refresh_data.anim_path_out);
	lv_anim_path_set_cb(&lvgl_refresh_data.anim_path_out, lv_anim_path_ease_out);
	//lv_anim_path_linear线性动画
	//lv_anim_path_step最后一步更改
	//lv_anim_path_ease_in开头很慢
	//lv_anim_path_ease_out最后慢
	//lv_anim_path_ease_in_out在开始和结束时也很慢
	//lv_anim_path_overshoot超出最终值
	//lv_anim_path_bounce从最终值反弹一点（就像撞墙一样）

	lv_anim_init(&lvgl_refresh_data.anim_out);
	lv_anim_set_ready_cb(&lvgl_refresh_data.anim_out, refresh_anim_out_ready_cb);
	lv_anim_set_exec_cb(&lvgl_refresh_data.anim_out, refresh_anim_out_cb);
	lv_anim_set_values(&lvgl_refresh_data.anim_out, lv_obj_get_y(*lvgl_refresh_data.fu), 0);
	lv_anim_set_time(&lvgl_refresh_data.anim_out, 200);
	lv_anim_set_var(&lvgl_refresh_data.anim_out, *lvgl_refresh_data.fu);

	lv_anim_set_path(&lvgl_refresh_data.anim_out, &lvgl_refresh_data.anim_path_out);
	lv_anim_start(&lvgl_refresh_data.anim_out);
#endif
}

int lvgl_refresh_get_sta(void)
{
	return lvgl_refresh_data.status;
}

